/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.broker.slave;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.broker.BrokerController;
import org.apache.rocketmq.broker.loadbalance.MessageRequestModeManager;
import org.apache.rocketmq.broker.subscription.SubscriptionGroupManager;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.TopicConfig;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
import org.apache.rocketmq.remoting.protocol.body.ConsumerOffsetSerializeWrapper;
import org.apache.rocketmq.remoting.protocol.body.MessageRequestModeSerializeWrapper;
import org.apache.rocketmq.remoting.protocol.body.SubscriptionGroupWrapper;
import org.apache.rocketmq.remoting.protocol.body.TopicConfigAndMappingSerializeWrapper;
import org.apache.rocketmq.store.config.StorePathConfigHelper;
import org.apache.rocketmq.store.timer.TimerCheckpoint;
import org.apache.rocketmq.store.timer.TimerMetrics;

/**
 *
 */
public class SlaveSynchronize {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerName.BROKER_LOGGER_NAME);

    private final BrokerController brokerController;

    /**
     * volatile变量，保持线程间的可见性
     */
    private volatile String masterAddr = null;

    public SlaveSynchronize(BrokerController brokerController) {
        this.brokerController = brokerController;
    }

    public String getMasterAddr() {
        return masterAddr;
    }

    /**
     * 设置broker的master节点地址
     * @param masterAddr
     */
    public void setMasterAddr(String masterAddr) {
        // 如果master地址发生变化，则打印日志
        if (!StringUtils.equals(this.masterAddr, masterAddr)) {
            LOGGER.info("Update master address from {} to {}", this.masterAddr, masterAddr);
            this.masterAddr = masterAddr;
        }
    }

    /**
     * 同步各种配置信息
     */
    public void syncAll() {
        // 同步主题配置信息
        this.syncTopicConfig();
        // 同步消费者偏移量
        this.syncConsumerOffset();
        // 同步延迟消息偏移量
        this.syncDelayOffset();
        // 同步订阅组配置信息
        this.syncSubscriptionGroupConfig();
        // 同步消息请求模式
        this.syncMessageRequestMode();

        // 如果启用了时间轮（默认启用）
        if (brokerController.getMessageStoreConfig().isTimerWheelEnable()) {
            // 同步时间轮指标数据
            this.syncTimerMetrics();
        }
    }

    /**
     * 从远程地址拉取主题配置信息和主题队列映射信息，更新本地缓存信息，并对这些数据进行持久化。
     */
    private void syncTopicConfig() {
        // 获取master地址
        String masterAddrBak = this.masterAddr;
        // 如果备份master地址不是null，并且与记录的master地址不同，则
        if (masterAddrBak != null && !masterAddrBak.equals(brokerController.getBrokerAddr())) {
            try {
                // 从备份master地址获取所有主题配置信息
                TopicConfigAndMappingSerializeWrapper topicWrapper =
                        this.brokerController.getBrokerOuterAPI().getAllTopicConfig(masterAddrBak);
                // 对比记录的broker控制器的主题配置信息数据版本，如果与获取的不同
                if (!this.brokerController.getTopicConfigManager().getDataVersion()
                        .equals(topicWrapper.getDataVersion())) {
                    // 更新本地缓存的broker控制器主题配置信息的数据版本
                    this.brokerController.getTopicConfigManager().getDataVersion()
                            .assignNewOne(topicWrapper.getDataVersion());
                    // 获取主题配置表：key是主题名称，value是主题配置信息对象
                    ConcurrentMap<String, TopicConfig> newTopicConfigTable = topicWrapper.getTopicConfigTable();
                    // 获取本地缓存的主题配置表
                    ConcurrentMap<String, TopicConfig> topicConfigTable = this.brokerController.getTopicConfigManager().getTopicConfigTable();
                    // 遍历本地缓存的主题配置表
                    for (Iterator<Map.Entry<String, TopicConfig>> it = topicConfigTable.entrySet().iterator(); it.hasNext(); ) {
                        Map.Entry<String, TopicConfig> item = it.next();
                        // 找到新的主题配置表中不包含的条目删除
                        if (!newTopicConfigTable.containsKey(item.getKey())) {
                            it.remove();
                        }
                    }
                    // 将新主题配置表中的条目全部添加到缓存的主题配置表中
                    topicConfigTable.putAll(newTopicConfigTable);
                    // 持久化主题配置信息
                    this.brokerController.getTopicConfigManager().persist();
                }
                // 如果有缓存的主题队列映射数据表，且缓存数据的版本与新数据的版本不同
                if (topicWrapper.getTopicQueueMappingDetailMap() != null
                        && !topicWrapper.getMappingDataVersion().equals(this.brokerController.getTopicQueueMappingManager().getDataVersion())) {
                    // 更新本地缓存的主题队列映射数据表的数据版本
                    this.brokerController.getTopicQueueMappingManager().getDataVersion()
                            .assignNewOne(topicWrapper.getMappingDataVersion());
                    // 获取新的主题配置表
                    ConcurrentMap<String, TopicConfig> newTopicConfigTable = topicWrapper.getTopicConfigTable();
                    // 获取本地缓存的主题配置表
                    ConcurrentMap<String, TopicConfig> topicConfigTable = this.brokerController.getTopicConfigManager().getTopicConfigTable();
                    // 遍历主题配置表
                    for (Iterator<Map.Entry<String, TopicConfig>> it = topicConfigTable.entrySet().iterator(); it.hasNext(); ) {
                        Map.Entry<String, TopicConfig> item = it.next();
                        // 找到不在新数据中包含的条目，删除
                        if (!newTopicConfigTable.containsKey(item.getKey())) {
                            it.remove();
                        }
                    }
                    // 将新的主题配置表中的条目添加到本地缓存
                    topicConfigTable.putAll(newTopicConfigTable);
                    // 持久化主题队列映射信息
                    this.brokerController.getTopicQueueMappingManager().persist();
                }
                LOGGER.info("Update slave topic config from master, {}", masterAddrBak);
            } catch (Exception e) {
                LOGGER.error("SyncTopicConfig Exception, {}", masterAddrBak, e);
            }
        }
    }

    /**
     * 同步消费者偏移量信息、本地数据的版本号，并对消费者偏移量数据执行持久化
     */
    private void syncConsumerOffset() {
        // 缓存当前记录的master地址
        String masterAddrBak = this.masterAddr;
        // 如果缓存的地址信息与当前对象保存的信息不同，表示有其他线程同步过了，操作取消
        if (masterAddrBak != null && !masterAddrBak.equals(brokerController.getBrokerAddr())) {
            try {
                // 从远程地址获取消费者偏移量序列化封装对象
                ConsumerOffsetSerializeWrapper offsetWrapper =
                        this.brokerController.getBrokerOuterAPI().getAllConsumerOffset(masterAddrBak);
                // 将获取的偏移量表数据全部添加到本地缓存的偏移量表中
                this.brokerController.getConsumerOffsetManager().getOffsetTable()
                        .putAll(offsetWrapper.getOffsetTable());
                // 更新本地消费者偏移量数据的数据版本
                this.brokerController.getConsumerOffsetManager().getDataVersion().assignNewOne(offsetWrapper.getDataVersion());
                // 持久化消费者偏移量信息
                this.brokerController.getConsumerOffsetManager().persist();
                LOGGER.info("Update slave consumer offset from master, {}", masterAddrBak);
            } catch (Exception e) {
                LOGGER.error("SyncConsumerOffset Exception, {}", masterAddrBak, e);
            }
        }
    }

    /**
     * 同步延迟消息偏移量
     */
    private void syncDelayOffset() {
        // 首先缓存当前master地址
        String masterAddrBak = this.masterAddr;
        // 如果本地缓存的master地址与broker控制器记录的master地址不同，则需要同步数据
        if (masterAddrBak != null && !masterAddrBak.equals(brokerController.getBrokerAddr())) {
            try {
                // 从master获取延迟消息偏移量
                String delayOffset =
                        this.brokerController.getBrokerOuterAPI().getAllDelayOffset(masterAddrBak);
                if (delayOffset != null) {
                    // 如果获取成功，则
                    // 获取本地存储延迟消息偏移量数据文件位置
                    String fileName =
                            StorePathConfigHelper.getDelayOffsetStorePath(this.brokerController
                                    .getMessageStoreConfig().getStorePathRootDir());
                    try {
                        // 首先将现有数据备份到备份文件中，然后将新内容写到文件中
                        MixAll.string2File(delayOffset, fileName);
                        // 持久化完成，加载到本地缓存中
                        this.brokerController.getScheduleMessageService().loadWhenSyncDelayOffset();
                    } catch (IOException e) {
                        LOGGER.error("Persist file Exception, {}", fileName, e);
                    }
                }
                LOGGER.info("Update slave delay offset from master, {}", masterAddrBak);
            } catch (Exception e) {
                LOGGER.error("SyncDelayOffset Exception, {}", masterAddrBak, e);
            }
        }
    }

    /**
     * 从master同步订阅组配置信息
     */
    private void syncSubscriptionGroupConfig() {
        // 获取本地缓存的master地址
        String masterAddrBak = this.masterAddr;
        // 如果缓存的master地址与broker控制器中的master地址不同，则需要同步数据
        if (masterAddrBak != null && !masterAddrBak.equals(brokerController.getBrokerAddr())) {
            try {
                // 向master发起请求获取订阅组封装对象
                SubscriptionGroupWrapper subscriptionWrapper =
                        this.brokerController.getBrokerOuterAPI()
                                .getAllSubscriptionGroupConfig(masterAddrBak);

                // 如果缓存的订阅组数据版本与新数据不同
                if (!this.brokerController.getSubscriptionGroupManager().getDataVersion()
                        .equals(subscriptionWrapper.getDataVersion())) {
                    // 获取订阅组管理器
                    SubscriptionGroupManager subscriptionGroupManager =
                            this.brokerController.getSubscriptionGroupManager();
                    // 更新本地订阅组数据版本
                    subscriptionGroupManager.getDataVersion().assignNewOne(
                            subscriptionWrapper.getDataVersion());
                    // 清空订阅组表数据
                    subscriptionGroupManager.getSubscriptionGroupTable().clear();
                    // 将新的订阅组表数据缓存到本地
                    subscriptionGroupManager.getSubscriptionGroupTable().putAll(
                            subscriptionWrapper.getSubscriptionGroupTable());
                    // 持久化订阅组表数据
                    subscriptionGroupManager.persist();
                    LOGGER.info("Update slave Subscription Group from master, {}", masterAddrBak);
                }
            } catch (Exception e) {
                LOGGER.error("SyncSubscriptionGroup Exception, {}", masterAddrBak, e);
            }
        }
    }

    /**
     * 从master同步消息请求模式
     */
    private void syncMessageRequestMode() {
        // 记录缓存的master地址
        String masterAddrBak = this.masterAddr;
        // 如果有缓存的master地址且与broker控制器的地址不同，则需要同步数据
        if (masterAddrBak != null && !masterAddrBak.equals(brokerController.getBrokerAddr())) {
            try {
                // 向master发起请求获取消息请求模式的序列化封装对象
                MessageRequestModeSerializeWrapper messageRequestModeSerializeWrapper =
                        this.brokerController.getBrokerOuterAPI().getAllMessageRequestMode(masterAddrBak);
                // 获取消息请求模式管理器
                MessageRequestModeManager messageRequestModeManager =
                        this.brokerController.getQueryAssignmentProcessor().getMessageRequestModeManager();
                // 清空消息请求模式表
                messageRequestModeManager.getMessageRequestModeMap().clear();
                // 将获取的新数据放到缓存的消息请求模式表
                messageRequestModeManager.getMessageRequestModeMap().putAll(
                        messageRequestModeSerializeWrapper.getMessageRequestModeMap()
                );
                // 持久化消息请求模式数据
                messageRequestModeManager.persist();
                LOGGER.info("Update slave Message Request Mode from master, {}", masterAddrBak);
            } catch (Exception e) {
                LOGGER.error("SyncMessageRequestMode Exception, {}", masterAddrBak, e);
            }
        }
    }

    /**
     * 同步时间轮检查点
     */
    public void syncTimerCheckPoint() {
        // 获取本地缓存的master地址
        String masterAddrBak = this.masterAddr;
        if (masterAddrBak != null) {
            try {
                // 如果存在时间轮消息存储
                if (null != brokerController.getMessageStore().getTimerMessageStore()) {
                    // 从远程地址获取时间轮检查点数据
                    TimerCheckpoint checkpoint = this.brokerController.getBrokerOuterAPI().getTimerCheckPoint(masterAddrBak);
                    // 如果当前有时间轮检查点数据
                    if (null != this.brokerController.getTimerCheckpoint()) {
                        // 设置最后一次读取时间为新数据的最后一次读取时间
                        this.brokerController.getTimerCheckpoint().setLastReadTimeMs(checkpoint.getLastReadTimeMs());
                        // 将master时间轮队列偏移量更新为新数据的master时间轮队列偏移量
                        this.brokerController.getTimerCheckpoint().setMasterTimerQueueOffset(checkpoint.getMasterTimerQueueOffset());
                    }
                }
            } catch (Exception e) {
                LOGGER.error("syncTimerCheckPoint Exception, {}", masterAddrBak, e);
            }
        }
    }

    /**
     * 同步时间轮指标数据
     */
    private void syncTimerMetrics() {
        // 获取缓存的master地址
        String masterAddrBak = this.masterAddr;
        if (masterAddrBak != null) {
            try {
                // 如果本地有时间消息存储
                if (null != brokerController.getMessageStore().getTimerMessageStore()) {
                    // 向master发起请求获取时间轮指标序列化封装对象
                    TimerMetrics.TimerMetricsSerializeWrapper metricsSerializeWrapper =
                            this.brokerController.getBrokerOuterAPI().getTimerMetrics(masterAddrBak);
                    // 如果数据版本与新数据的数据版本不同，则
                    if (!brokerController.getMessageStore().getTimerMessageStore().getTimerMetrics().getDataVersion().equals(metricsSerializeWrapper.getDataVersion())) {
                        // 更新时间轮消息存储中时间轮指标的数据版本
                        this.brokerController.getMessageStore().getTimerMessageStore().getTimerMetrics().getDataVersion().assignNewOne(metricsSerializeWrapper.getDataVersion());
                        // 清空时间轮消息存储中时间轮指标数据的计时指标表数据
                        this.brokerController.getMessageStore().getTimerMessageStore().getTimerMetrics().getTimingCount().clear();
                        // 设置时间轮消息存储的时间轮指标数据中的计时指标表数据
                        this.brokerController.getMessageStore().getTimerMessageStore().getTimerMetrics().getTimingCount().putAll(metricsSerializeWrapper.getTimingCount());
                        // 数据持久化
                        this.brokerController.getMessageStore().getTimerMessageStore().getTimerMetrics().persist();
                    }
                }
            } catch (Exception e) {
                LOGGER.error("SyncTimerMetrics Exception, {}", masterAddrBak, e);
            }
        }
    }
}
